package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.micrometer.core.instrument.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 96558
 * @Date 2021/1/9 20:15
 * @Version 1.0
 **/
@Slf4j
@Configuration
@RestControllerAdvice   //Springmvc异常处理拦截注解
public class ExceptionCatch {

    /**
    * 解决项目中所有的异常处理
     *
     * @return
    * */
    @ExceptionHandler(Exception.class)  //exception 所有子类
    public ResponseResult exception(Exception ex){
        //记录日志
        log.error("ExceptionCatch ex:{}",ex);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"您的网络异常，请稍后重试");
    }
    @ExceptionHandler(CustomException.class)
    public ResponseResult custException(CustomException ex){
     //   System.out.println(ex);
        log.error("CustomException ex:{}",ex);
        AppHttpCodeEnum codeEnum = ex.getAppHttpCodeEnum();
        return ResponseResult.errorResult(codeEnum);
    }











}
